package com.huawei.hms.framework.network.restclient.hwhttp.cronet;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hianalytics.EditableMetrics;
import com.huawei.hms.framework.network.restclient.hianalytics.EditableMetricsTime;
import java.util.Collection;
import java.util.concurrent.Executor;
import org.chromium.net.RequestFinishedInfo;
import org.chromium.net.RequestFinishedInfo.Listener;
import org.chromium.net.RequestFinishedInfo.Metrics;

public class CronetEventListener extends Listener {
    private static final String DEFAULT_PROTOCOL = "unknown";
    private static final long DEFEAULT_TIME = -1;
    private static final String TAG = "CronetEventListener";

    public CronetEventListener(Executor executor) {
        super(executor);
    }

    private CronetRequestTask getCronetRequestTask(RequestFinishedInfo requestFinishedInfo) {
        Collection annotations = requestFinishedInfo.getAnnotations();
        Object obj = (annotations == null || annotations.isEmpty()) ? null : annotations.toArray()[0];
        if (!(obj instanceof CronetRequestTask)) {
            return null;
        }
        Logger.v(TAG, "call getAnnotations return CronetRequestTask instance");
        return (CronetRequestTask) obj;
    }

    public void onRequestFinished(RequestFinishedInfo requestFinishedInfo) {
        if (requestFinishedInfo == null) {
            Logger.e(TAG, "requestInfo == null at onRequestFinished");
            return;
        }
        try {
            CronetRequestTask cronetRequestTask = getCronetRequestTask(requestFinishedInfo);
            if (cronetRequestTask == null) {
                Logger.e(TAG, "fail to get CronetRequestTask from getAnnotations");
                return;
            }
            CronetRequestFinishedInfo cronetRequestFinishedInfo = (CronetRequestFinishedInfo) cronetRequestTask.getRequestFinishedInfo();
            if (cronetRequestFinishedInfo == null) {
                Logger.w(TAG, "requestFinishedInfo == null at onRequestFinished");
                return;
            }
            dumpRequestFinishInfo(requestFinishedInfo, cronetRequestFinishedInfo);
            cronetRequestTask.onRequestFinish();
        } catch (Throwable th) {
            Logger.e(TAG, "onRequestFinished occur exception, exception name:" + th.getClass().getSimpleName());
        }
    }

    private void dumpRequestFinishInfo(RequestFinishedInfo requestFinishedInfo, CronetRequestFinishedInfo cronetRequestFinishedInfo) {
        if (requestFinishedInfo == null || cronetRequestFinishedInfo == null) {
            Logger.e(TAG, "invalid parameters");
            return;
        }
        cronetRequestFinishedInfo.setUrl(requestFinishedInfo.getUrl());
        cronetRequestFinishedInfo.setException(requestFinishedInfo.getException());
        dumpMetricsTime(requestFinishedInfo.getMetrics(), cronetRequestFinishedInfo.getMetricsTime());
        dumpMetrics(requestFinishedInfo.getMetrics(), requestFinishedInfo, cronetRequestFinishedInfo.getMetrics());
    }

    private void dumpMetrics(Metrics metrics, RequestFinishedInfo requestFinishedInfo, EditableMetrics editableMetrics) {
        long j = DEFEAULT_TIME;
        if (metrics == null || requestFinishedInfo == null || editableMetrics == null) {
            Logger.e(TAG, "invalid parameters");
            return;
        }
        String str;
        editableMetrics.setRequestByteCount(metrics.getSentByteCount() == null ? DEFEAULT_TIME : metrics.getSentByteCount().longValue());
        if (metrics.getReceivedByteCount() != null) {
            j = metrics.getReceivedByteCount().longValue();
        }
        editableMetrics.setResponseByteCount(j);
        if (requestFinishedInfo.getResponseInfo() == null) {
            str = "unknown";
        } else {
            str = requestFinishedInfo.getResponseInfo().getNegotiatedProtocol();
        }
        editableMetrics.setProtocol(str);
    }

    private void dumpMetricsTime(Metrics metrics, EditableMetricsTime editableMetricsTime) {
        long j = DEFEAULT_TIME;
        if (metrics == null || editableMetricsTime == null) {
            Logger.e(TAG, "invalid parameters");
            return;
        }
        editableMetricsTime.setCallStartTime(metrics.getRequestStart() == null ? DEFEAULT_TIME : metrics.getRequestStart().getTime());
        editableMetricsTime.setCallEndTime(metrics.getRequestEnd() == null ? DEFEAULT_TIME : metrics.getRequestEnd().getTime());
        editableMetricsTime.setDnsStartTime(metrics.getDnsStart() == null ? DEFEAULT_TIME : metrics.getDnsStart().getTime());
        editableMetricsTime.setDnsEndTime(metrics.getDnsEnd() == null ? DEFEAULT_TIME : metrics.getDnsEnd().getTime());
        editableMetricsTime.setConnectStartTime(metrics.getConnectStart() == null ? DEFEAULT_TIME : metrics.getConnectStart().getTime());
        editableMetricsTime.setConnectEndTime(metrics.getConnectEnd() == null ? DEFEAULT_TIME : metrics.getConnectEnd().getTime());
        editableMetricsTime.setSecureConnectStartTime(metrics.getSslStart() == null ? DEFEAULT_TIME : metrics.getSslStart().getTime());
        editableMetricsTime.setSecureConnectEndTime(metrics.getSslEnd() == null ? DEFEAULT_TIME : metrics.getSslEnd().getTime());
        editableMetricsTime.setRequestHeadersStartTime(metrics.getSendingStart() == null ? DEFEAULT_TIME : metrics.getSendingStart().getTime());
        editableMetricsTime.setRequestHeadersEndTime(metrics.getSendingEnd() == null ? DEFEAULT_TIME : metrics.getSendingEnd().getTime());
        editableMetricsTime.setResponseHeadersStartTime(metrics.getResponseStart() == null ? DEFEAULT_TIME : metrics.getResponseStart().getTime());
        editableMetricsTime.setResponseBodyEndTime(metrics.getRequestEnd() == null ? DEFEAULT_TIME : metrics.getRequestEnd().getTime());
        if (editableMetricsTime instanceof CronetMetricsTime) {
            CronetMetricsTime cronetMetricsTime = (CronetMetricsTime) editableMetricsTime;
            cronetMetricsTime.setTtfb(metrics.getTtfbMs() == null ? DEFEAULT_TIME : metrics.getTtfbMs().longValue());
            if (metrics.getTotalTimeMs() != null) {
                j = metrics.getTotalTimeMs().longValue();
            }
            cronetMetricsTime.setTotalTime(j);
        }
    }
}
